// pages/schoolList/schoolList.js
var request = require('../../operation/operation.js')
var bxt = require('../../utils/bxt.js')

var region;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    inputShowed: false,
    inputVal: "",
    schools:[],
    showShool:false,
    region:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    region = getApp().globalData.param;
    this.initRegionView(region)
    this.searchSchool('')
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 单击学校列表处理函数
   */
  selectSchool:function(event) {
    var school = event.currentTarget.dataset.school;
    getApp().notificationCenter.post(bxt.EDIT_SCHOOL_MESSAGE, { school })

    wx.navigateBack({
      
    })
  },

  showInput: function () {
    this.setData({
      inputShowed: true
    });
  },

  hideInput: function () {
    this.setData({
      inputVal: "",
      inputShowed: false
    });
  },

  clearInput: function () {
    this.setData({
      inputVal: ""
    });
  },

  inputTyping: function (e) {
    this.searchSchool(e.detail.value);
  },

  /**
   * 请求服务器模糊查询学校信息
   */
  searchSchool:function(name) {
    var that = this;
    request.getRequest('/schools?province=' + encodeURIComponent(region[0]) + '&city=' + encodeURIComponent(region[1]) + '&county=' + encodeURIComponent(region[2]) + '&name=' + encodeURIComponent(name), {}, true)
      .then(res => {
        var data = res.data;
        if (request.SUCCESSED == data.status) {
          that.setData({
            showShool:true,
            schools:data.items
          })
        }
      }).catch(e => {
        console.log(e)
      })
  },

  initRegionView:function(region) {
    this.setData({
      region:region
    })
  }

})